﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IService;
using DFGK_WMS.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace DFGK.Service
{
    public class RoutewayService : BaseService, IRoutewayService
    {
        private readonly IRoutewayRepository _routewayRepository;
        private readonly IWarehouseRepository _warehouseRepository;
        private readonly IShelfRepository _shelfRepository;
        private readonly IUnitOfWork _unitOfWork;
        public RoutewayService(IMapper mapper, IRoutewayRepository _routewayRepository, IWarehouseRepository warehouseRepository, IShelfRepository shelfRepository, IUnitOfWork _unitOfWork) : base(mapper)
        {
            this._routewayRepository = _routewayRepository;
            this._unitOfWork = _unitOfWork;
            _shelfRepository = shelfRepository;
            _warehouseRepository = warehouseRepository;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool AddRrouteway(Routeway obj, out string message)
        {
            message = string.Empty;
            var robj = _routewayRepository.QueryIsEntityWhere(x => x.Code == obj.Code);
            if (robj != null)
            {
                message = "编码已存在！";
                return false;
            }
            if(obj.WhId<=0)
            {
                message = "请选择库房！";
                return false;
            }
            //库房
            var wobj = _warehouseRepository.QueryEntityById(obj.WhId);
            if (wobj != null && wobj.Status == 1)
            {
                obj.WhCode = wobj.Code;
                obj.WhName = wobj.Name;
            }
            else
            {
                message = "库房不存在或已被禁用！";
                return false;
            }
            _routewayRepository.AddEntity(obj);
            return _unitOfWork.SaveChangesEntity();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool DelRrouteway(long id)
        {
            var robj = _routewayRepository.QueryEntityById(id);
            if (robj != null)
            {
                _routewayRepository.DelEntity(robj);
                return _unitOfWork.SaveChangesEntity();
            }
            return false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Routeway? GetRroutewayById(long id)
        {
            return _routewayRepository.QueryEntityById(id);
        }

        /// <summary>
        /// 巷道列表显示
        /// </summary>
        /// <param name="xname"></param>
        /// <param name="wid"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<Routeway> GetRroutewayList(string? xname, long wid, int pageIndex, int pageSize, out int totalCount)
        {
            var rlist = _routewayRepository.GetEntityAlls();

            //巷道名称
            if (!string.IsNullOrEmpty(xname))
            {
                rlist = rlist.Where(x => x.Name.Contains(xname));
            }
            //库房
            if (wid != 0)
            {
                rlist = rlist.Where(x => x.WhId == wid);
            }

            var list = _routewayRepository.GetEntityAlls(rlist, pageIndex, pageSize, out totalCount, x => x.CreateDate, false);
            return list.ToList();
        }

        /// <summary>
        /// 根据货架拿到所属巷道
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public int? GetShelfRoutewayId(long id)
        {
            var robj = _shelfRepository.QueryEntityById(id);
            if (robj != null && robj.RoutewayId!=null)
                return robj.Number;
            return null;
        }

        /// <summary>
        /// 根据库房获取巷道
        /// </summary>
        /// <param name="wid"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<Routeway> GetWhIdRoutewayList(long wid)
        {
            var list = _routewayRepository.GetEntityWhereAll(x => x.WhId == wid);
            return list.ToList();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool UpdRrouteway(Routeway obj, out string message)
        {
            message = "";
            var robj = _routewayRepository.QueryIsEntityWhere(x => x.Id != obj.Id && x.Code == obj.Code);
            if (robj != null)
            {
                message = "编码已存在！";
                return false;
            }
            if (obj.WhId <= 0)
            {
                message = "请选择库房！";
                return false;
            }
            //库房
            var wobj =_warehouseRepository.QueryEntityById(obj.WhId);
            if (wobj != null && wobj.Status == 1)
            {
                obj.WhCode = wobj.Code;
                obj.WhName = wobj.Name;
            }
            else
            {
                message = "库房不存在或已被禁用！";
                return false;
            }
            _routewayRepository.UpdEntity(obj);
            return _unitOfWork.SaveChangesEntity();
        }
    }
}
